package com.jtbd.service;

import com.jtbd.constants.UserTypeEnum;
import com.jtbd.dto.http.UserStudentHttpDto;
import com.jtbd.dto.db.UserStudentInfo;
import com.jtbd.handler.UserLoginInfoHandler;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

/**
 * 学生基础业务操作相关的service
 */
@Service
public class StudentBaseOperateService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserStudentService userStudentService;
    @Resource
    private UserLoginInfoHandler userRedisHandler;

    public UserStudentHttpDto studentLogin(HttpServletResponse response, String phone, String password) {
        Long uid = userInfoService.userLogin(response, phone, UserTypeEnum.STUDENT.getCode(), password);
        UserStudentInfo userStudentInfo = userStudentService.getUserStudentInfo(uid);
        userStudentInfo.getUserInfo().setPassword(null);
        userRedisHandler.studentLogin(userStudentInfo, response);

//        // 将用户登录的token信息保存到redis中
//        JwtPayloadEntity jwtPayloadEntity = new JwtPayloadEntity(uid, userStudentInfo.getUserInfo().getUsername(), userAuthDetails.getPassword(), userAuthDetails.getRoleIds(), UUID.randomUUID().toString());
//        response.setHeader(JwtUtil.JWT_KEY, jwtUtil.generateToken(jwtPayloadEntity));
//        // 保存用户登录的token信息到redis中
//        redisClient.set(JwtUtil.redisTokenKey(String.valueOf(uid)), jwtPayloadEntity.getSignChar());
        return UserStudentHttpDto.getUserStudentHttpDto(userStudentInfo);
    }

    public int studentRegister(UserStudentHttpDto studentInfo) {
        return userStudentService.addUserStudentInfo(UserStudentHttpDto.getUserStudentInfo(studentInfo));
    }
}
